//
//  KKChatTextView.m
//  KKLiveDemo
//
//  Created by leo on 2017/7/29.
//  Copyright © 2017年 leo. All rights reserved.
//

#import "KKChatTextView.h"
#import "KKLiveChatEmotionManager.h"

@interface KKChatTextView()

@property (nonatomic, strong) NSMutableAttributedString *attributedString;

@end

@implementation KKChatTextView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self private_initTextViewUI];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        [self private_initTextViewUI];
    }
    return self;
}

- (void)dealloc {
    
    [self removeObserver: self forKeyPath: @"contentSize"];
}

#pragma mark - Public methods
- (void)kk_appendEmoticonImageIndex:(NSInteger)imageIndex {
    UIImage *image = [[KKLiveChatEmotionManager manager] kk_chatEmoticonWithIndex: imageIndex];
    if (image == nil) {
        return;
    }
    else {
        //  表情
        KKTextAttachment *attch = [[KKTextAttachment alloc] init];
        // 表情图片
        attch.image = image;
        attch.imageIndex = imageIndex;
        // 设置图片大小
        attch.bounds = CGRectMake(0, -6, 20, 20);
        // 创建带有图片的富文本
        NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attch];
        
        NSMutableAttributedString *strM = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
        
        {
            [strM replaceCharactersInRange:self.selectedRange withAttributedString:imageText];
            [strM addAttribute:NSFontAttributeName value: self.font range:NSMakeRange(self.selectedRange.location, 1)];
            self.attributedText = strM;
            self.selectedRange = NSMakeRange(self.selectedRange.location + 1,0);
        }
        
        self.attributedString = [[NSMutableAttributedString alloc] initWithAttributedString: strM];
    }
}

- (void)kk_cleanAllText {
    self.attributedString = nil;
    self.attributedText = nil;
    self.text = nil;
}

#pragma mark - Private methodsfd

- (void)private_initTextViewUI {
    
    self.delegate = self;
    self.font = [UIFont systemFontOfSize: 14];
    self.returnKeyType = UIReturnKeySend;
    self.layer.cornerRadius = 3.0;
    self.layer.masksToBounds = YES;
    self.layer.borderColor = [UIColor colorWithRed: 216.0/255.0 green: 215.0/255.0 blue:223.0/255.0 alpha: 1.0].CGColor;
    self.layer.borderWidth = 0.8;
    [self addObserver: self forKeyPath: @"contentSize" options: NSKeyValueObservingOptionNew context: nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString: @"contentSize"]) {
        CGSize contentSize = [change[NSKeyValueChangeNewKey] CGSizeValue];
        
        NSInteger row = (int)contentSize.height / 15;
        CGFloat height = row * 15.0;
        if (row == 2) {
            height += 3;
        }
        
        if (self.changeContentHeightHandle) {
            self.changeContentHeightHandle(height);
        }
    }
}

#pragma mark - UITextViewDelegate methods
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if ([text isEqualToString: @"\n"]) {
        NSString *sendText = textView.text;
        NSAttributedString *coreText = textView.attributedText;
        textView.text = @"";
        textView.attributedText = nil;
        [textView resignFirstResponder];
        
        [self kk_cleanAllText];
        
        if (coreText && self.didEndEditCoreTextHandle && sendText.length) {
            self.didEndEditCoreTextHandle(coreText);
        }
        else if (self.didEndEditTextHandle && sendText.length) {
            self.didEndEditTextHandle(sendText);
        }
        else if (sendText.length == 0 || coreText.length == 0) {
            [textView resignFirstResponder];
        }
        self.attributedString = nil;
        return false;
    }
    return true;
}

@end

@implementation KKChatCoreTextImageItem
@end

@implementation KKTextAttachment
@end
